package org.afun.oldhand.forum.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import org.afun.oldhand.R;
import org.afun.oldhand.base.BaseFragment;
import org.afun.oldhand.forum.activity.ForumMsgActivity;
import org.afun.oldhand.forum.adapter.ForumBottomItemAdapter;
import org.afun.oldhand.forum.adapter.ForumTopItemAdapter;
import org.afun.oldhand.forum.javaBean.ItemBottomContentBean;
import org.afun.oldhand.forum.javaBean.ItemTopBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ForumFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ForumFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private Forum0Fragment forum0Fragment;
    private Forum1Fragment forum1Fragment;
    private Button forum_zixun;
    private Button forum_dongtai;
    private ImageButton forum_msg;


    public ForumFragment() {
        // Required empty public constructor
    }

    public static ForumFragment newInstance() {
        ForumFragment fragment = new ForumFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return initView();
    }

    @Override
    public View initView() {
        if (view == null) {
            view = View.inflate(mContext, R.layout.fragment_forum, null);
        }
        // top
        forum_msg = view.findViewById(R.id.forum_msg);
        forum_zixun = view.findViewById(R.id.forum_zixun);
        forum_dongtai = view.findViewById(R.id.forum_dongtai);
        forum_msg.setOnClickListener(this);
        forum_zixun.setOnClickListener(this);
        forum_dongtai.setOnClickListener(this);


        forum0Fragment = new Forum0Fragment();
        forum1Fragment = new Forum1Fragment();
        replaceFragment(forum0Fragment);

        return view;
    }

    // 动态切换fragment
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transation = fragmentManager.beginTransaction();
        transation.replace(R.id.forumFrameLayout, fragment);
        transation.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forum_msg:
                Intent intent = new Intent(mContext, ForumMsgActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.forum_zixun:
                replaceFragment(forum0Fragment);
                break;
            case R.id.forum_dongtai:
                replaceFragment(forum1Fragment);
                break;
        }
    }
}